﻿using System.Collections;
using System.Collections.Generic;
using Consts;
using Dto;
using UI;
using UI.Msg;
using UnityEngine;
using UnityEngine.UI;

public class RegistPanel : UIBase
{
    private Button btnRegist;

    private Button btnClose;

    private InputField inputAccount;

    private InputField inputPassword;
    
    private InputField inputRePassword;
    
    private PromptMsg _promptMsg;


    private void Awake()
    {
        Bind(UIEvent.Regist_PANEL_ACTIVE);
    }

    // Start is called before the first frame update
    void Start()
    {
        btnRegist = transform.Find("BtnRegist").GetComponent<Button>();
        btnClose = transform.Find("BtnClose").GetComponent<Button>();
        inputAccount = transform.Find("InputAccount").GetComponent<InputField>();
        inputPassword = transform.Find("InputPassword").GetComponent<InputField>();
        inputRePassword = transform.Find("InputRePassword").GetComponent<InputField>();
        btnRegist.onClick.AddListener(Regist);
        btnClose.onClick.AddListener(CloseClick);
        _promptMsg = new PromptMsg();
        _reqBase = new ReqBase(SocketConst.ACCOUNT,AccountCodeConst.REGIST);

        setPanelActive(false);
    }

    // Update is called once per frame
    void Update()
    {
    }
    
        
    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case UIEvent.Regist_PANEL_ACTIVE:
                setPanelActive((bool)message);
                break;
            default:
                break;
        }
    }
    
    public override void OnDestroy()
    {
        base.OnDestroy();
        btnRegist.onClick.RemoveListener(Regist);
        btnClose.onClick.RemoveListener(CloseClick);
    }

    AccountDto dto = new AccountDto();
    private ReqBase _reqBase ;
    private void Regist()
    {
        if (string.IsNullOrEmpty(inputAccount.text))
        {
            _promptMsg.Change("注册的用户名不能为null",Color.red);
            Dispatch(AreaCode.UI,UIEvent.PROMPT_MSG,_promptMsg);
            return;
        }

        if (string.IsNullOrEmpty(inputPassword.text))
        {
            _promptMsg.Change("密码不能为null",Color.red);
            Dispatch(AreaCode.UI,UIEvent.PROMPT_MSG,_promptMsg);
            return;
        }

        if (string.IsNullOrEmpty(inputRePassword.text) || (!inputRePassword.text.Equals(inputPassword.text)))
        {
            _promptMsg.Change("重复密码不能为null || 密码与重复密码不一致",Color.red);
            return;
        }
        AccountDto accountDto = new AccountDto(inputAccount.text,inputPassword.text); 
        _reqBase.Change(accountDto);
        Dispatch(AreaCode.NET,0,_reqBase);
    }

    private void CloseClick()
    {
        setPanelActive(false);
    }
}